package com.example.android_app;

import com.pervasive.project.unified.data.model.request.AuthenticationRequest;
import com.pervasive.project.unified.data.model.response.AuthenticationResponse;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

	// Declare our Views, so we can access them later
	private EditText etUsername;
	private Button btnLogin;
	private Button btnCancel;
	private Button resultshow;
	//private TextView lblResult;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Get the EditText and Button References
		etUsername = (EditText) findViewById(R.id.username);
		btnLogin = (Button) findViewById(R.id.login_button);
		btnCancel = (Button) findViewById(R.id.cancel_button);
		resultshow = (Button) findViewById(R.id.urlview);
		//lblResult = (TextView) findViewById(R.id.result);

		btnCancel.setOnClickListener(new CancelButtonListener());
		btnLogin.setOnClickListener(new LoginButtonListener());
		resultshow.setOnClickListener(new ResultShowListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private AuthenticationRequest generateAuthReq()
	{
		return new AuthenticationRequest(etUsername.getText().toString());
	}
	
	private class LoginButtonListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
			// *** DUMMY SERVER CALL *** 
			AuthenticationResponse response = com.dummy.server.Authentication.authenticate(generateAuthReq());
			if(response.getGameAvail()==true)
			{
				if (response.getAuthResp() == true)
				{
					Intent intent = new Intent();
					intent.setClassName("com.example.android_app", "com.example.android_app.GameActivity");
					Bundle bundle = new Bundle();
					bundle.putString("username", etUsername.getText().toString());
					intent.putExtras(bundle);
					startActivity(intent);
					//will not need the toast but will redirect to the main game page 
				//	Toast.makeText(MainActivity.this, "Login succesful!! Start Game  ",Toast.LENGTH_LONG).show();
				}
				else
				{
					Toast.makeText(MainActivity.this, "Incorrect Login  ",Toast.LENGTH_LONG).show();
				}
			}
			else
			{
				Toast.makeText(MainActivity.this, "Game Unavailable",Toast.LENGTH_LONG).show();
			}
			
		}
	}

		private class CancelButtonListener implements OnClickListener {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}

		}
		
		private class ResultShowListener implements OnClickListener {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("http://156.56.94.8:8080/services/GetResults");
				Intent intent = new Intent(Intent.ACTION_VIEW,uri);
				startActivity(intent);
						
			}

		}

	
}
